package com.zjs.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjs.dao.UserDao;
import com.zjs.pojo.User;
import com.zjs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import util.ResultStatus;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    private UserDao userdao;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public ResultStatus Login(User user) {
        //输入的验证码
        String code = user.getCode();
        //后台获取的验证码
        ValueOperations<String, String> value = redisTemplate.opsForValue();
        String code1 = value.get("code");
        //比较验证码是否一样
        if (!code.equalsIgnoreCase(code1)){
            return ResultStatus.FTLED(300,"验证码不正确");
        }
        String userName = user.getUserName();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUserName, userName);
        wrapper.last("limit 1");
        User user1 = userdao.selectOne(wrapper);
        if (user1==null){
            return ResultStatus.FTLED(300,"用户名错误");
        }else {
            if (user.getUserPassword().equals(user1.getUserPassword())){
                Map<String, Object> paylod = new HashMap<>();
                paylod.put("userid",user.getUserId());
                paylod.put("username",user.getUserName());
                String token = JWTUtil.createToken(paylod, "123456".getBytes());
                HashMap<Object, Object> map = new HashMap<>();
                user.setUserPassword("");
                map.put("user",user);
                map.put("token",token);
                value.set("token",token,60, TimeUnit.SECONDS);
                return  ResultStatus.SUCCESS(map);
            }else{
                return ResultStatus.FTLED(300,"密码错误");
            }
        }
    }

    @Override
    public ResultStatus getCode(User user) {
        //手机号
        String tel = user.getUserTel();
        ValueOperations<String, String> value = redisTemplate.opsForValue();
        String code = RandomUtil.randomNumbers(4);
        System.err.println("手机号为："+tel+"的用户收到短信："+code);
        value.set(tel,code);
        return ResultStatus.SUCCESS(code);
    }

    @Override
    public ResultStatus addUser(User user) {
        User user1 = userdao.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserName, user.getUserName()).last("limit 1"));
        if (user1!=null){
            return  ResultStatus.FTLED(300,"用户名被占用");
        }
        User user2 = userdao.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserTel, user.getUserTel()).last("limit 1"));
        if (user2!=null){
            return  ResultStatus.FTLED(300,"该手机号已注册");
        }
        userdao.insert(user);
        return ResultStatus.SUCCESS("注册成功",user);
    }
}
